#!/bin/bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

######################################################################
#
# This script will use the fabric-ca-server and fabric-ca-client
# binaries to dynamically generate the CLI flags and the
# configuration files for both server and the client and place
# them in the docs/source folder. These files are referenced for
# in the fabric-ca documentation when talking about CLI and the
# configuration files
#
######################################################################

echo "Making docs..."

######################################################################
# Command Line Flag Generation
######################################################################

function generateGlobalCLI {
    echo "Generating Global CLI..."

    # RST specific syntax to indicate a code a block
    echo -e "=======================" >> servercli.rst
    echo -e "Fabric-CA Server's CLI" >> servercli.rst
    echo -e "=======================\n" >> servercli.rst
    echo -e "::\n" >> servercli.rst

    echo -e "=======================" > clientcli.rst
    echo -e "Fabric-CA Client's CLI" >> clientcli.rst
    echo -e "=======================\n" >> clientcli.rst
    echo -e "::\n" >> clientcli.rst

    # Direct the CLI help message to a temp file
    fabric-ca-server > servercli_temp.rst
    fabric-ca-client > clientcli_temp.rst

    # Sanitize the CLI file to remove any machine specific information and provide a generic CLI
    sed -i -e 's/home directory (default.*/home directory (default "$HOME\/.fabric-ca-client")/' clientcli_temp.rst
    sed -i -e 's/enrollment (default.*/enrollment (default "$HOSTNAME")/' clientcli_temp.rst
    sed -i -e 's/home directory (default.*/home directory (default "\/etc\/hyperledger\/fabric-ca")/' servercli_temp.rst

    # Insert a few spaces in front of all the lines in temp files created above (RST formatting purposes)
    sed -i -e 's/^/    /' servercli_temp.rst
    sed -i -e 's/^/    /' clientcli_temp.rst

    # Append temp files to ones created earlier
    cat servercli_temp.rst >> servercli.rst
    cat clientcli_temp.rst >> clientcli.rst
}

function generateIdentityCLI {
    echo "Generating Identity Command CLI..."

    echo -e "\nIdentity Command" >> clientcli.rst
    echo -e "==================\n" >> clientcli.rst
    echo -e "::\n" >> clientcli.rst

    fabric-ca-client identity -h >> identity_cmd.rst
    sed -i -e '/Global Flags:/,$d' identity_cmd.rst
    printf '%s\n\n' '-----------------------------' >> identity_cmd.rst

    fabric-ca-client identity add -h > identity_add_cmd.rst
    sed -i -e '/Global Flags:/,$d' identity_add_cmd.rst
    cat identity_add_cmd.rst >> identity_cmd.rst
    printf '%s\n\n' '-----------------------------' >> identity_cmd.rst

    fabric-ca-client identity list -h > identity_list_cmd.rst
    sed -i -e '/Global Flags:/,$d' identity_list_cmd.rst
    cat identity_list_cmd.rst >> identity_cmd.rst
    printf '%s\n\n' '-----------------------------' >> identity_cmd.rst

    fabric-ca-client identity modify -h > identity_modify_cmd.rst
    sed -i -e '/Global Flags:/,$d' identity_modify_cmd.rst
    cat identity_modify_cmd.rst >> identity_cmd.rst
    printf '%s\n\n' '-----------------------------' >> identity_cmd.rst

    fabric-ca-client identity remove -h > identity_remove_cmd.rst
    sed -i -e '/Global Flags:/,$d' identity_remove_cmd.rst
    cat identity_remove_cmd.rst >> identity_cmd.rst

    sed -i -e 's/^/    /' identity_cmd.rst
    cat identity_cmd.rst >> clientcli.rst
}

function generateAffiliationCLI {
    echo "Generating Affiliation Command CLI..."

    echo -e "\nAffiliation Command" >> clientcli.rst
    echo -e "=====================\n" >> clientcli.rst
    echo -e "::\n" >> clientcli.rst

    fabric-ca-client affiliation -h >> affiliation_cmd.rst
    sed -i -e '/Global Flags:/,$d' affiliation_cmd.rst
    printf '%s\n\n' '-----------------------------' >> affiliation_cmd.rst

    fabric-ca-client affiliation add -h > affiliation_add_cmd.rst
    sed -i -e '/Global Flags:/,$d' affiliation_add_cmd.rst
    cat affiliation_add_cmd.rst >> affiliation_cmd.rst
    printf '%s\n\n' '-----------------------------' >> affiliation_cmd.rst

    fabric-ca-client affiliation list -h > affiliation_list_cmd.rst
    sed -i -e '/Global Flags:/,$d' affiliation_list_cmd.rst
    cat affiliation_list_cmd.rst >> affiliation_cmd.rst
    printf '%s\n\n' '-----------------------------' >> affiliation_cmd.rst

    fabric-ca-client affiliation modify -h > affiliation_modify_cmd.rst
    sed -i -e '/Global Flags:/,$d' affiliation_modify_cmd.rst
    cat affiliation_modify_cmd.rst >> affiliation_cmd.rst
    printf '%s\n\n' '-----------------------------' >> affiliation_cmd.rst

    fabric-ca-client affiliation remove -h > affiliation_remove_cmd.rst
    sed -i -e '/Global Flags:/,$d' affiliation_remove_cmd.rst
    cat affiliation_remove_cmd.rst >> affiliation_cmd.rst

    sed -i -e 's/^/    /' affiliation_cmd.rst
    cat affiliation_cmd.rst >> clientcli.rst
}

function generateCertificateCLI {
    echo "Generating Certificate Command CLI..."

    echo -e "\nCertificate Command" >> clientcli.rst
    echo -e "=====================\n" >> clientcli.rst
    echo -e "::\n" >> clientcli.rst

    fabric-ca-client certificate -h >> certificate_cmd.rst
    sed -i -e '/Global Flags:/,$d' certificate_cmd.rst
    printf '%s\n\n' '-----------------------------' >> certificate_cmd.rst

    fabric-ca-client certificate list -h > certificate_list_cmd.rst
    sed -i -e '/Global Flags:/,$d' certificate_list_cmd.rst
    cat certificate_list_cmd.rst >> certificate_cmd.rst

    sed -i -e 's/^/    /' certificate_cmd.rst
    cat certificate_cmd.rst >> clientcli.rst
}


fabric_ca=$GOPATH/src/github.com/hyperledger/fabric-ca
docsdir=$fabric_ca/docs/source
export PATH=$PATH:$fabric_ca/bin

if ! "${fabric_ca}/scripts/metrics_doc.sh"; then
    echo "Metrics doc generation failed"
    exit 1
fi

temp=$docsdir/temp
rm -rf $temp
mkdir -p $temp
cd $temp

generateGlobalCLI
generateIdentityCLI
generateAffiliationCLI
generateCertificateCLI

mv servercli.rst $docsdir/servercli.rst
mv clientcli.rst $docsdir/clientcli.rst

rm $temp/* # Clean up artificats

######################################################################
# Configuration File Generation
######################################################################

# Start server and enroll client to generate the configuration files
export FABRIC_CA_HOME=$temp

echo "Starting server to generate server configuration file"
fabric-ca-server start -b abc:d > /dev/null 2>&1 &
sleep 1 # Wait for server to start before client enrolls
echo "Client enrolling to generate client configuration file"
fabric-ca-client enroll -u http://abc:d@localhost:7054 > /dev/null 2>&1 &
sleep 1 # Wait for client to finish enrolling
echo "Stopping server..."
pkill -9 fabric-ca-server > /dev/null 2>&1 & # Stop the server

# RST specific syntax to indicate a code a block
echo -e "Fabric-CA Server's Configuration File" > serverconfig.rst
echo -e "======================================\n" >> serverconfig.rst
echo -e "::\n" >> serverconfig.rst

echo -e "Fabric-CA Client's Configuration File" > clientconfig.rst
echo -e "======================================\n" >> clientconfig.rst
echo -e "::\n" >> clientconfig.rst

# Sanitize the configuration files to remove any machine specific information and provide a generic config file
sed -e 's/cn:.*/cn: <<<COMMONNAME>>>/' -e 's/pathlength:.*/pathlength: <<<PATHLENGTH>>>/' -e 's/abc/<<<adminUserName>>>/' -e 's/pass:.*/pass: <<<adminPassword>>>/' -e 's/'"$HOSTNAME"'/<<<MYHOST>>>/' -e 's/version:.*/version: <<<VERSION>>>/' fabric-ca-server-config.yaml > server-config.yaml
sed -e 's/cn:.*/cn: <<<ENROLLMENT_ID>>>/' -e 's/'"$HOSTNAME"'/<<<MYHOST>>>/' -e 's/url:.*/url: <<<URL>>>/' fabric-ca-client-config.yaml > client-config.yaml

# Insert a few spaces in front of all the lines in temp files created above (RST formatting purposes)
sed -i -e 's/^/    /' server-config.yaml
sed -i -e 's/^/    /' client-config.yaml

# Append temp files to ones created earlier
cat server-config.yaml >> serverconfig.rst
cat client-config.yaml >> clientconfig.rst

mv serverconfig.rst $docsdir/serverconfig.rst
mv clientconfig.rst $docsdir/clientconfig.rst

rm -rf $temp # Clean up artificats

echo "Doc generation completed"

# git status by itself just returns exit code, not if there any modifications.
# Using the --porcelain flag returns information on what files got modified, if any.
# Only checking status under docs/source folder
cd $docsdir
if [[ $(git status . --porcelain --untracked-file=no) ]]; then
    echo "ERROR: New readme files generated, commit changes before doing push"
    exit 1
fi
